// 导出页面为PDF格式
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
export default {
  install(Vue, options) {
    Vue.prototype.getPdf = function (domID) {
      return new Promise(async (resolve1, reject2) => {
        //这是在界面上设置一个id
        //只下载id为pdfDom的内容
        html2Canvas(document.getElementById(domID), {
          //body是下载整个界面
          useCORS: true, //是否尝试使用CORS从服务器加载图像
          allowTaint: true,
          // dpi: 300, //解决生产图片模糊
          // width: 490, //canvas宽度
          // height: 240, //canvas高度
          // x: 0, //x坐标
          // y: 0, //y坐标
          // async: false, //是否异步解析和呈现元素
          // foreignObjectRendering: true, //是否在浏览器支持的情况下使用ForeignObject渲染
        }).then(function (canvas) {
          console.log(canvas);
          let contentWidth = canvas.width;
          let contentHeight = canvas.height;
          let pageHeight = (contentWidth / 592.28) * 841.89;
          let leftHeight = contentHeight;
          let position = 0;
          let imgWidth = 595.28;
          let imgHeight = (592.28 / contentWidth) * contentHeight;
          let pageData = canvas.toDataURL("image/jpeg", 1.0);
          let PDF = new JsPDF("", "pt", "a4");
          if (leftHeight < pageHeight) {
            PDF.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
          } else {
            while (leftHeight > 0) {
              PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
              leftHeight -= pageHeight;
              position -= 841.89;
              if (leftHeight > 0) {
                PDF.addPage();
              }
            }
          }
          resolve1(PDF);
        });
      });
    };
  },
};
